import java.util.Arrays;
import java.util.Scanner;

public class Solution {
    public static int missingNumber(int[] nums) {
        int num = 0;
        int n = nums.length;
        for(int i = 0;i < nums.length;i++){
            num+=nums[i];
        }
        return n*(n+1)/2-num;
    }
    public static void main1(String[] args) {
        int[] nums = {3,1,0};
        int ret = missingNumber(nums);
        System.out.println("缺少的数字是"+ret);
    }
    public void rotate(int[] nums, int k) {
        /*
        反转数组的原地算法
        在分界点处将两边的数组反转一次，然后再总体反转一次即可
        时间复杂度:O(N) 空间复杂度:O(1)
         */
        int n = nums.length;
        // 将k取余
        k %= n;
        reverse(nums, 0, n - k - 1);
        reverse(nums, n - k, n - 1);
        reverse(nums, 0, n - 1);
    }

    // 反转数组指定索引的元素
    public void reverse(int[] nums, int start, int end) {
        int l = start, r = end;
        while (l < r) {
            int t = nums[l];
            nums[l] = nums[r];
            nums[r] = t;
            l++;
            r--;
        }
    }
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] array = {1,2,3,4,5,6,7};
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入右旋数字K");
        int k = scanner.nextInt();
        solution.rotate(array,k);
        System.out.println(Arrays.toString(array));
    }
}
